package com.ebelter.nb.ui.fragments.history;


import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.R;
import com.ebelter.nb.adapters.H_XueYa_A;
import com.ebelter.nb.beans.GetBP;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.utils.NbUtitls;


import static com.ebelter.btcomlib.common.Constants.IUser.AUTH_KEY;
import static com.ebelter.nb.ui.activitys.TrendActivity.Trend_XUEYA;


public class H_XueYa_Fragment extends BaseFragment {
    public static final String TAG = "H_XueYa_Fragment";

    ListView listView;
    H_XueYa_A adapter;

    RelativeLayout no_history_ly;

    private int startPage = 1;
    private int pageSize=10;

    private long lastLoadingTime;




    View listFootView;
    TextView noMoreTv;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_historys;
    }


    private String currentUser;
    @Override
    protected void initView() {
        super.initView();
        FV();
        adapter=new H_XueYa_A(getContext());
        listView.setAdapter(adapter);

        listFootView = View.inflate(getContext(), R.layout.no_history, null);
        noMoreTv=listFootView.findViewById(R.id.no_data_tv);
        noMoreTv.setText(R.string.No_more);
        noMoreTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        noMoreTv.setTextColor(Color.parseColor("#ffcccccc"));
        listView.addFooterView(listFootView);
        ViewUtils.hideView(listFootView);

        currentUser = getArguments().getString("user","1");


        getHostoryListDatas(startPage,pageSize,currentUser);

        listView.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        if (System.currentTimeMillis()-lastLoadingTime>3000){
                            LogUtils.i(TAG,"---------加载更多");
                            lastLoadingTime=System.currentTimeMillis();
                            getHostoryListDatas(++startPage,pageSize,currentUser);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });





    }

    private void FV() {
        listView=mRootView.findViewById(R.id.history_lv);
        no_history_ly = findViewById(R.id.no_history_ly);
        ViewUtils.hideView(no_history_ly);

    }




    public void getHostoryListDatas(final int startPage,final int pageSize,String currentUser) {
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            displayNoHistoryLy();
            return;
        }
        showLoadingDialog(StringUtils.getResStr(R.string.loading));


        String authKey = NbUtitls.getLoginAuthkey();
        NbNetUtils.getInstance().getBloodPressurePage(this, authKey, startPage, pageSize,currentUser, new HttpResponse<GetBP>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, GetBP getBP, String s) {
                dissLoadingDialog();
                LogUtils.i(TAG,"---s = "+s);
                LogUtils.i(TAG,"---getBP = " + getBP);
                if (isRequestSuccess&&getBP.resultData!=null){
                    if (getBP.resultData.dataList.size()==0){
                        ViewUtils.displayView(listFootView);
                    }
                    if (adapter!=null)adapter.addData(getBP.resultData.dataList);
                }
                displayNoHistoryLy();
            }
        });




    }



    private void displayNoHistoryLy(){
        if (adapter!=null){
            if (adapter.getCount()==0){
                ViewUtils.displayView(no_history_ly);
                ViewUtils.goneView(noMoreTv);
            }else {
                ViewUtils.hideView(no_history_ly);
            }
        }
    }



}
